package com.leihub.xlfiletools.html.pojo;

import com.leihub.xlfiletools.html.annotation.DisplayField;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.math.BigDecimal;
import java.math.RoundingMode;

@AllArgsConstructor
@Data
@NoArgsConstructor
public class ConsolidatedTransactionData {

    private String tranCode;

    @DisplayField(name = "总笔数", group = 1, groupName = "1.ESB/ESF发送交易")
    private String totalSuccessfulTransactions;

    @DisplayField(name = "Percentage", group = 1, groupName = "1.ESB/ESF发送交易")
    private String successfulTransactionsPercentage;

    @DisplayField(name = "平均耗时", group = 1, groupName = "1.ESB/ESF发送交易")
    private String successfulTransactionsAverageTime;

    @DisplayField(name = "Success Rate", group = 1, groupName = "1.ESB/ESF发送交易")
    private BigDecimal successfulTransactionsRate;

    @DisplayField(name = "总笔数", group = 2, groupName = "2.分布式核心系统处理交易")
    private String totalQueryTransactions;

    @DisplayField(name = "Percentage", group = 2, groupName = "2.分布式核心系统处理交易")
    private String queryTransactionsPercentage;

    @DisplayField(name = "平均耗时", group = 2, groupName = "2.分布式核心系统处理交易")
    private String queryTransactionsAverageTime;

    @DisplayField(name = "Success Rate", group = 2, groupName = "2.分布式核心系统处理交易")
    private BigDecimal queryTransactionsRate;

    @DisplayField(name = "总笔数", group = 3, groupName = "3.转核心产品化系统处理交易")
    private String totalAccountingTransactions;

    @DisplayField(name = "Percentage", group = 3, groupName = "3.转核心产品化系统处理交易")
    private String accountingTransactionsPercentage;

    @DisplayField(name = "平均耗时", group = 3, groupName = "3.转核心产品化系统处理交易")
    private String accountingTransactionsAverageTime;

    @DisplayField(name = "Success Rate", group = 3, groupName = "3.转核心产品化系统处理交易")
    private BigDecimal accountingTransactionsRate;

    /**
     * Initializes a new instance of the class with computed success rates.
     *
     * @param tranCode                      the transaction code
     * @param totalSuccessfulTransactions   total successful transactions
     * @param successfulTransactionsPercentage successful transactions percentage
     * @param successfulTransactionsAverageTime successful transactions average time
     * @param totalQueryTransactions        total query transactions
     * @param queryTransactionsPercentage   query transactions percentage
     * @param queryTransactionsAverageTime  query transactions average time
     * @param totalAccountingTransactions   total accounting transactions
     * @param accountingTransactionsPercentage accounting transactions percentage
     * @param accountingTransactionsAverageTime accounting transactions average time
     */
    public ConsolidatedTransactionData(String tranCode, String totalSuccessfulTransactions, String successfulTransactionsPercentage, String successfulTransactionsAverageTime, String totalQueryTransactions, String queryTransactionsPercentage, String queryTransactionsAverageTime, String totalAccountingTransactions, String accountingTransactionsPercentage, String accountingTransactionsAverageTime) {
        this.tranCode = tranCode;
        this.totalSuccessfulTransactions = totalSuccessfulTransactions;
        this.successfulTransactionsPercentage = successfulTransactionsPercentage;
        this.successfulTransactionsAverageTime = successfulTransactionsAverageTime;
        this.totalQueryTransactions = totalQueryTransactions;
        this.queryTransactionsPercentage = queryTransactionsPercentage;
        this.queryTransactionsAverageTime = queryTransactionsAverageTime;
        this.totalAccountingTransactions = totalAccountingTransactions;
        this.accountingTransactionsPercentage = accountingTransactionsPercentage;
        this.accountingTransactionsAverageTime = accountingTransactionsAverageTime;
        initializeRates();
    }

    /**
     * Initialize the rates based on the total transactions and percentage values.
     */
    private void initializeRates() {
        this.successfulTransactionsRate = calculateRate(totalSuccessfulTransactions, successfulTransactionsPercentage);
        this.queryTransactionsRate = calculateRate(totalQueryTransactions, queryTransactionsPercentage);
        this.accountingTransactionsRate = calculateRate(totalAccountingTransactions, accountingTransactionsPercentage);
    }

    /**
     * Helper method to calculate the success rate.
     *
     * @param totalTransactions the total number of transactions as a String
     * @param percentage the success percentage as a String
     * @return the success rate as a BigDecimal
     */
    private BigDecimal calculateRate(String totalTransactions, String percentage) {
        try {
            BigDecimal total = new BigDecimal(totalTransactions.replace("笔", ""));
            BigDecimal percent = new BigDecimal(percentage.replace("%", ""));
            return total.multiply(percent).divide(BigDecimal.valueOf(100), 2, RoundingMode.HALF_UP);
        } catch (NumberFormatException e) {
            e.printStackTrace();
            return BigDecimal.ZERO;
        }
    }
}
